package com.ys.web.sys.oss.controller;

import com.ys.utils.aliyun.OssHandler;
import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.redis.aop.RedisCache;
import com.ys.utils.redis.aop.RedisType;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import com.ys.web.sys.oss.model.Oss;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/oss/")
public class OssController {

    @Autowired
    private OssManager ossManager;

    /**
     * @ 名称:  获取oss授权上传url
     * @ 参数： business 业务名
     * @ 返回值:
     */
    @RequestMapping("policy")
    public String ossPolicy(@RequestAttribute String uid, String business, @RequestParam(defaultValue = "") String fileName) throws UnsupportedEncodingException {
        //验证入参
        if(StringHandler.isEmpty(business)){
            SystemError.wrapBs("2000","请求参数异常...");
        }
        //内存中获取业务配置的上传前缀
        Oss ossPo = ossManager.getOssPo(business);
        if(null == ossPo){
        	SystemError.wrapBs("2000","请配置路径策略...");
        }
        //拼装上窜路径前缀
        String url = ossPo.getUrl();
        if(StringHandler.isNotEmpty(ossPo.getUrlParam())){
            String urlParam = ossPo.getUrlParam();
            String[] params = urlParam.split(",");
            for(String s : params){
                url = ossManager.loadUrl(url, s);
            }
            //判断上传路径是否包含用户id
            url =  url.contains("${uid}") ? StringUtils.replaceOnce(url, "${uid}", uid) : url;
        }
        //获取令牌信息
        return ResponseHandler.success(URLEncoder.encode(StringHandler.getString(new OssHandler(Config.pp,1).info(url,fileName, ossPo.getExpireTime())),"UTF-8"));
    }

    /**
     * @ 名称:  获取oss临时授权
     * @ 返回值:
     */
    @RequestMapping("sts")
    @RedisCache(type = RedisType.None,timeOut = 3000)
    public String ossSTS(){
        //获取令牌信息
        return ResponseHandler.success(new OssHandler(Config.pp,1).sts());
    }

    /**
     * @ 名称:  获取oss授权上传url
     * @ 参数： business 业务名
     * @ 返回值:
     */
    @RequestMapping("policy/v2")
    public String ossPolicyV2(@RequestAttribute String uid, String business, @RequestParam(defaultValue = "") String fileName) throws UnsupportedEncodingException {
        //验证入参
        if(StringHandler.isEmpty(business)){
            SystemError.wrapBs("2000","请求参数异常...");
        }
        //内存中获取业务配置的上传前缀
        Oss ossPo = ossManager.getOssPo(business);
        if(null == ossPo){
            SystemError.wrapBs("2000","请配置路径策略...");
        }
        //拼装上窜路径前缀
        String url = ossPo.getUrl();
        if(StringHandler.isNotEmpty(ossPo.getUrlParam())){
            String urlParam = ossPo.getUrlParam();
            String[] params = urlParam.split(",");
            for(String s : params){
                url = ossManager.loadUrl(url, s);
            }
            //判断上传路径是否包含用户id
            url =  url.contains("${uid}") ? StringUtils.replaceOnce(url, "${uid}", uid) : url;
        }
        //获取令牌信息
        return ResponseHandler.success(URLEncoder.encode(StringHandler.getString(new OssHandler(Config.pp,2).info(url,fileName, ossPo.getExpireTime())),"UTF-8"));
    }

    /**
     * @ 名称:  获取oss临时授权
     * @ 返回值:
     */
    @RequestMapping("sts/v2")
    @RedisCache(type = RedisType.None,timeOut = 3000)
    public String ossSTSV2(@RequestParam(defaultValue = "2") int type){
        //获取令牌信息
        return ResponseHandler.success(new OssHandler(Config.pp,type).sts());
    }
}
